//
//  HXAntipathyVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/12.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXAntipathyVC.h"
#import "HX_Macro.h"
#import "HXAntipathyModel.h"
#import "HXAntipathyTCell.h"
#import "HXSuitableModel.h"

@interface HXAntipathyVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *suitable;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *imageHeader;
@end

@implementation HXAntipathyVC
- (NSMutableArray *)suitable {
    if (!_suitable) {
        _suitable = [NSMutableArray array];
    }
    return _suitable;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatHeadImage];
    [self tableView];
    [self getDishesSuitableWithModelDishes_id:self.dishes_id];
}
- (void)creatHeadImage {
    self.imageHeader = [[UIImageView alloc] init];
    _imageHeader.HX_width = SCREEN_WIDTH;
    _imageHeader.HX_height = 200;
    _imageHeader.HX_x = 0;
    _imageHeader.HX_y = 0;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        self.tableView.bounces = NO;
        _tableView.separatorStyle = NO;
        _tableView.tableHeaderView = _imageHeader;
        [_tableView registerNib:[UINib nibWithNibName:@"HXAntipathyTCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HXAntipathyTCell"];
    }
    return _tableView;
}
- (void)getDishesSuitableWithModelDishes_id:(NSString *)dishes_id {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"methodName=DishesSuitable&dishes_id=%@&version=4.40&", dishes_id];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSDictionary *dicMaterial = dicData[@"material"];
        HXAntipathyModel *antipath = [HXAntipathyModel ModelWithDic:dicMaterial];
        [self.imageHeader sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", antipath.image]]];
        [self creatHeadImage];
        [self.suitable addObject:antipath];
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    HXAntipathyModel *antipath = [self.suitable firstObject];
    return antipath.suitable_with.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXAntipathyTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXAntipathyTCell"];
    HXAntipathyModel *antipathModel = [self.suitable firstObject];
    [cell.imageHeader sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [antipathModel.suitable_with[indexPath.row] image]]]];
    cell.name.text = [antipathModel.suitable_with[indexPath.row] material_name];
    cell.detail.text = [antipathModel.suitable_with[indexPath.row] suitable_desc];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HXAntipathyModel *antipath = [self.suitable firstObject];
    UIView *viewSection = [[UIView alloc] init];
    viewSection.HX_width = SCREEN_WIDTH;
    viewSection.HX_x = 0;
    viewSection.HX_y = 0;
    viewSection.backgroundColor = [UIColor whiteColor];
    UILabel *labelSection = [[UILabel alloc] init];
    labelSection.HX_width = SCREEN_WIDTH;
    labelSection.HX_x = 10;
    labelSection.text = [NSString stringWithFormat:@"与%@搭配相宜的食材", antipath.material_name];
    labelSection.font = [UIFont systemFontOfSize:15];
    labelSection.centerY = viewSection.centerY;
    labelSection.HX_height = 50;
    [viewSection addSubview:labelSection];
    return viewSection;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
